<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';
include dirname(dirname(dirname(__FILE__))) . '/include/xmppChat.php';
class webServiceFinalStepInviteGuest extends core {

    public function process() {
        $user_id = intval($_POST['user_id']);
        $access_token = $_POST['access_token'];
        $arr_guest_list_fb = $_POST['guest_list_from_fb'];
        $arr_guest_list_email = $_POST['guest_list_from_email'];
        $event_id = intval($_POST['event_id']);
        $subject = $this->db->escape(strip_tags($_POST['subject']));
        $message = $this->db->escape(strip_tags($_POST['message']));
        $fb_user_id='';
        $data = $this->db->get_row("SELECT * FROM users WHERE id=".$user_id);
        if ($data) {
            $fb_user_id= $data->facebook_id;
        }else{
             $this->_JSONResponse(array('error' => 1, 'msg' => "Oops, something went wrong. Please try again!"));
        }
        $event = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND `user_id`='$user_id'");
        if ($event) {
            $query_invite_messages = 'INSERT INTO invite_guest_messages (subject,content,event_id) VALUES ("' . $subject . '","' . $message . '",' . $event_id . ');';
            if ($this->db->query($query_invite_messages)) {
                $invite_message_id = $this->db->insert_id;
                //validate guest list fb
                $arr_guest_list_fb = explode(",", $arr_guest_list_fb);
                foreach ($arr_guest_list_fb as &$guest_id_fb) {
                    $guest_id_fb = $this->db->escape($guest_id_fb);
                }
                //select data guest
                $array_guest_id_fb = implode(",", $arr_guest_list_fb);

                $data_guest_fb = $this->db->get_results("SELECT g.guest_id,g.fbid FROM guests g, guest_event ge WHERE ge.is_invited = 0  AND g.guest_id= ge.guest_id AND ge.guest_id IN ($array_guest_id_fb) AND ge.event_id=$event_id");
                //save fb_guest_id,fb_user_id to send message by facebook chat
                foreach ($data_guest_fb as $guest) {
                    $guest_id = $guest->guest_id;
                    $fb_guest_id = $guest->fbid;
                    $query = 'INSERT INTO invite_guest_queue (invite_message_id,guest_id,fb_guest_id,fb_user_id) VALUES (' . $invite_message_id . ',' . $guest_id . ',"'.$fb_guest_id.'","'.$fb_user_id.'");';

                    $this->db->query($query);
                }
                //validate guest list email
                $arr_guest_list_email = explode(",", $arr_guest_list_email);
                foreach ($arr_guest_list_email as &$guest_id_email) {
                    $guest_id_email = $this->db->escape($guest_id_email);
                }
                //select data guest
                $array_guest_id_email = implode(",", $arr_guest_list_email);
                $data_guest_email = $this->db->get_results("SELECT g.email,g.guest_id FROM guests g, guest_event ge WHERE ge.is_invited = 0 AND g.email!='' AND g.guest_id= ge.guest_id AND ge.guest_id IN ($array_guest_id_email) AND ge.event_id=$event_id");
               
                foreach ($data_guest_email as $guest) {
                    $guest_id = $guest->guest_id;
                    $email = $guest->email;
                    $query = 'INSERT INTO invite_guest_queue (invite_message_id,guest_email,guest_id) VALUES (' . $invite_message_id . ',"' . $email . '",' . $guest_id . ');';
                    $this->db->query($query);
                   
                }
                $domain= $this->GetAbsoluteURLFolder();
                if($access_token==''){
                    $access_token='null';
                }
                exec("php /var/www/html/weddinghelper/cronjob/inviteGuest.php $event_id $access_token $domain > /dev/null  &");
            } else {
                $this->_JSONResponse(array('error' => 1, 'msg' => "Oops, something went wrong. Please try again!"));
            }
            $this->_JSONResponse(array('error' => 0, 'msg' => 'Invitation has been sent'));
        } else {
            $this->_JSONResponse(array('error' => 1, 'msg' => "Event does not exist!"));
        }
    }

}

$processor = new webServiceFinalStepInviteGuest();
$processor->process();
?>